//
//  GQTodayRecommandViewController.m
//  TingMusic
//
//  Created by sunny on 16/4/21.
//  Copyright © 2016年 sunny. All rights reserved.
//

#import "GQTodayRecommandViewController.h"
#import "GQTodayRecommandCell.h"
#import "GQTodayRemmandItem.h"
#import "GQRecommandCellButton.h"
#import "GQRecommandTableViewItem.h"
@interface GQTodayRecommandViewController () <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (weak, nonatomic) IBOutlet UILabel *titleTopLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleBottomLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeightConstraint;
@property (nonatomic,assign)CGFloat FirstOffsetY;
@property (nonatomic, strong)NSArray *itemArray;
@property (weak, nonatomic) IBOutlet UILabel *totalLabel;
/** 显示日历的按钮 */
@property (weak, nonatomic) IBOutlet GQRecommandCellButton *calenderButton;

@end
static NSString * const ID = @"TodayRecommandCell";
@implementation GQTodayRecommandViewController
//返回按钮
- (IBAction)backButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSArray *)itemArray
{
    if (!_itemArray) {
        _itemArray = [NSArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.contentInset = UIEdgeInsetsMake(120 + 44, 0, 0, 0);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 70;
    self.FirstOffsetY = self.tableView.contentOffset.y;
    self.view.autoresizingMask = UIViewAutoresizingNone;
    self.navigationController.navigationBarHidden = YES;
//      注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"GQTodayRecommandCell" bundle:nil] forCellReuseIdentifier:ID];
    
//    设置日历上的文字和图片
    self.calenderButton.font = [UIFont systemFontOfSize:15];
    self.calenderButton.centerLabel.text = self.recommandTableViewItem.day;
    self.calenderButton.centerLabel.textColor = [UIColor whiteColor];

    [[[UIImageView alloc]init] sd_setImageWithURL:[NSURL URLWithString:self.recommandTableViewItem.icon]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image == nil) return ; //图片下载失败
            
        [self.calenderButton setImage:image forState:UIControlStateNormal];
        
    }];
    
    
//    发送请求，加载数据
    [self loadData];
    
}


#pragma mark - 发送请求，加载数据
- (void)loadData
{

    [GQHttpRequest GET:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.7.3.0&channel=xiaomi&operator=2&method=baidu.ting.song.userRecSongList&format=json&page_no=1&page_size=60" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        self.itemArray = [GQTodayRemmandItem mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"list"]];
        self.totalLabel.text = [NSString stringWithFormat:@"共%zd首歌",self.itemArray.count];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GQTodayRecommandCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    GQTodayRemmandItem *todayRemmandItem = self.itemArray[indexPath.row];
    cell.todayRemmandItem = todayRemmandItem;
    return cell;
}

#pragma mark - scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    获取tableView的滚动量(相对于原始的)
    CGFloat offsetY = scrollView.contentOffset.y - self.FirstOffsetY;
    self.imageViewHeightConstraint.constant = 120 - offsetY;
    CGFloat minConst = CGRectGetMaxY(self.backButton.frame);
    if (self.imageViewHeightConstraint.constant <= minConst) {
        self.imageViewHeightConstraint.constant = minConst;
        self.titleTopLabel.hidden = NO;
        self.titleBottomLabel.hidden = YES;
    } else {
        self.titleTopLabel.hidden = YES;
        self.titleBottomLabel.hidden = NO;
    }
}

@end
